using System;
using System.Collections.Generic;
using System.Text;
using Org.Eurekaa.DNJ.Ext.Common;
using System.Web.UI.WebControls;
using Org.Eurekaa.DNJ.Ext.PluginsData.Jplayer;
using System.Web.UI.HtmlControls;
using System.Reflection;
using System.IO;
using System.Web.UI;
using Org.Eurekaa.DNJ.Helpers;
using System.ComponentModel;
using Org.Eurekaa.DNJ.UI.WebControls;
using Org.Eurekaa.DNJ.Collections;
using Org.Eurekaa.DNJ.Ext.DNJSettings;

namespace Org.Eurekaa.DNJ.Ext.UI.WebControls
{

    [Serializable()]
    public class JPlayerFile
    {
        private string _url;
        [Editor(typeof(System.Web.UI.Design.UrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public string Url
        {
            get { return _url; }
            set { _url = value; }
        }
    }

    [ToolboxData("<{0}:Jplayer Height=\"100px\" runat=server></{0}:Jplayer>")]
//    [ToolboxBitmap(typeof(Org.Eurekaa.DNJ.UI.Resources.icons.DNJUIIcons), "JPlayer.bmp")]
    [Designer(typeof(Org.Eurekaa.DNJ.UI.Design.WebControls.JQueryUIControlDesigner))]
    [ParseChildren(true)]
    [PersistChildren(false)]
    public class Jplayer : JQueryUIControl, IDNJPlugin
    {
        private HtmlInputHidden tbxPlayedTime;


        private DNJGenericCollection<JPlayerFile> _playList = new DNJGenericCollection<JPlayerFile>();
        [DefaultValue(""), Category("$.DNJ Settings"), DisplayName("PlayList")]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [MergableProperty(false)]
        public DNJGenericCollection<JPlayerFile> PlayList {
            get
            {
                return _playList;
            }
        }

        public Jplayer()
        {
            EnsureID();            
        }
        //protected override void Render(System.Web.UI.HtmlTextWriter writer)
        //{
        //    if (DesignMode)
        //    {
        //        writer.Write("<div style=\"ui-widget ui-helper-clearfix\"><b>Jplayer</b></div>");
        //    }
        //    else base.Render(writer);
        //    //writer.Write("<b>DNJ implementation of Jplayer (alpha)</b>");            
        //}
        protected override void CreateChildControls()
        {
            //Controls.Clear();
            string id = this.ID + "Ctrl";

            HtmlGenericControl ctrlDiv = new HtmlGenericControl("div");            
            ctrlDiv.ID = id;

            Assembly assembly = Assembly.GetExecutingAssembly();
            string[] names = assembly.GetManifestResourceNames();
            Stream stream = assembly.GetManifestResourceStream("Org.Eurekaa.DNJ.Ext.PluginsData.Jplayer.PlayerTemplate.htm");
            StreamReader streamReader = new StreamReader(stream, Encoding.UTF8);
            String tpl = streamReader.ReadToEnd();

            tpl = tpl.Replace("JplayerID", this.ID + "_" + ctrlDiv.UniqueID);
            ctrlDiv.InnerHtml = tpl;

            //handle played time between postbacks
            tbxPlayedTime = new HtmlInputHidden();
            tbxPlayedTime.Attributes.Add("class", "playedTime");
            ctrlDiv.Controls.Add(tbxPlayedTime);

            if (DesignMode)
            {
                stream = assembly.GetManifestResourceStream("Org.Eurekaa.DNJ.Ext.PluginsData.Jplayer.common.css");
                streamReader = new StreamReader(stream, Encoding.UTF8);
                String scriptTpl = streamReader.ReadToEnd();
                HtmlGenericControl style = new HtmlGenericControl("style");
                style.InnerText = scriptTpl;
                Controls.Add(style);
            }
            if (!DesignMode)
            {
                PageHelper.RegisterHeaderCSS(Page.ClientScript.GetWebResourceUrl(typeof(JplayerResources), "Org.Eurekaa.DNJ.Ext.PluginsData.Jplayer.common.css"), Page.Header);
                //PageHelper.RegisterHeaderScript(Page.ClientScript.GetWebResourceUrl(typeof(JplayerResources), "Org.Eurekaa.DNJ.Ext.PluginsData.Jplayer.jquery.jplayer.js"), Page.Header);
                Org.Eurekaa.DNJ.Config.ResourcesConfig.AddResource("jplayer", "Org.Eurekaa.DNJ.Ext.PluginsData.Jplayer.jquery.jplayer.js");

                PageHelper.RegisterDNJResource("jplayer", this.Page.Header);

                stream = assembly.GetManifestResourceStream("Org.Eurekaa.DNJ.Ext.PluginsData.Jplayer.script.js");
                streamReader = new StreamReader(stream, Encoding.UTF8);
                String scriptTpl = streamReader.ReadToEnd();

                scriptTpl = scriptTpl.Replace("JplayerID", this.ID + "_" + ctrlDiv.UniqueID);
                scriptTpl = scriptTpl.Replace("JplayerSWFURL", Page.ClientScript.GetWebResourceUrl(typeof(JplayerResources), "Org.Eurekaa.DNJ.Ext.PluginsData.Jplayer.Jplayer.swf") + "&");

                if (this.PlayList[0] != null)
                    scriptTpl = scriptTpl.Replace("{File}", this.PlayList[0].Url.Replace("~/", ""));


                //Org.Eurekaa.DNJ.Config.ResourcesConfig.AddResourceInnerScript("jplayer", scriptTpl);

                PageHelper.AppendJQueryScript(scriptTpl, Page.Header);
            }
            


            

            Controls.Add(ctrlDiv);
            base.CreateChildControls();

        }
        

        #region IDNJPlugin Members
        public string Version()
        {
            return "0.2.4";
        }

        public bool Embedded()
        {
            return true;
        }
        #endregion
    }
}
